package com.itcs.rongyun.rongsdk;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.itcs.rongyun.utils.Constants;
import com.itcs.rongyun.utils.HttpUtil;


public class RongCloud {
	
	private static final String url = Constants.RONGCLOUD_URI;
	
	private static final String utf8 = "UTF-8";
	 
	private static final String appKey = Constants.RONGCLOUD_APPKEY;
	
	private static final String appSecret = Constants.RONGCLOUD_APPSECRET;
	
	private static final FormatType format = FormatType.json;

	/**
	 * 用户登录
	 * @param userId       app中唯一标识
	 * @param userName 名称
	 * @param portraitUri 头像
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult getToken(String userId, String userName, String portraitUri)  {

		HttpURLConnection conn;
		try {
			conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret, url + "/user/getToken." + format.toString());
			StringBuilder sb = new StringBuilder();
			sb.append("userId=").append(URLEncoder.encode(userId, utf8));
			sb.append("&name=").append(URLEncoder.encode(userName==null?"":userName, utf8));
			sb.append("&portraitUri=").append(URLEncoder.encode(portraitUri==null?"":portraitUri, utf8));
			HttpUtil.setBodyParameter(sb, conn);

			return HttpUtil.returnResult(conn);
		
		} catch (MalformedURLException e) {
			return new SdkHttpResult(-1, e.getMessage());
		} catch (ProtocolException e) {
			return new SdkHttpResult(-1, e.getMessage());
		} catch (IOException e) {
			return new SdkHttpResult(-1, e.getMessage());
		}catch (Exception e) {
			return new SdkHttpResult(-1, e.getMessage());
		}
		
	}
	 

	/**
	 * 检查用户在线状态
	 * @param userId 
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult checkOnline(String userId) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,appSecret,url + "/user/checkOnline." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 刷新用户信息
	 * @param userId
	 * @param userName
	 * @param portraitUri
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult refreshUser(String userId, String userName, String portraitUri) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,appSecret, url + "/user/refresh." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		if (userName != null) {
			sb.append("&name=").append(URLEncoder.encode(userName, utf8));
		}
		if (portraitUri != null) {
			sb.append("&portraitUri=").append(
					URLEncoder.encode(portraitUri, utf8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 封禁用户
	 * @param userId
	 * @param minute
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult blockUser(String userId, int minute) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, url + "/user/block." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		sb.append("&minute=").append(
				URLEncoder.encode(String.valueOf(minute), utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 解禁用户 
	 * @param userId 
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult unblockUser(String userId) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,appSecret, url + "/user/unblock." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
	
	/**
	 * 获取被封禁用户
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult queryBlockUsers() throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,appSecret,url + "/user/block/query." + format.toString());

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 添加用户到黑名单
	 * @param userId
	 * @param blackUserIds
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult blackUser(String userId, List<String> blackUserIds)throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,appSecret,url + "/user/blacklist/add." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		if (blackUserIds != null) {
			for (String blackId : blackUserIds) {
				sb.append("&blackUserId=").append(
						URLEncoder.encode(blackId, utf8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 从黑名单移除用户
	 * @param userId
	 * @param blackUserIds
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult unblackUser(String userId, List<String> blackUserIds)throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret, url + "/user/blacklist/remove." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		if (blackUserIds != null) {
			for (String blackId : blackUserIds) {
				sb.append("&blackUserId=").append(
						URLEncoder.encode(blackId, utf8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 获取黑名单用户
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult QueryblackUser(String userId) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret, url + "/user/blacklist/query." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 创建群
	 * @param userIds
	 * @param groupId
	 * @param groupName
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult createGroup(List<String> userIds, String groupId, String groupName) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,appSecret, url + "/group/create." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, utf8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName==null?"":groupName, utf8));
		if (userIds != null) {
			for (String id : userIds) {
				sb.append("&userId=").append(URLEncoder.encode(id, utf8));
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 加入群
	 * @param userId
	 * @param groupId
	 * @param groupName
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult joinGroup(
			String userId, String groupId, String groupName)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, url + "/group/join." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		sb.append("&groupId=").append(URLEncoder.encode(groupId, utf8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName==null?"":groupName, utf8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 批量加入群
	 * @param userIds
	 * @param groupId
	 * @param groupName
	 * @return
	 */
	public static SdkHttpResult joinGroupBatch(List<String> userIds, String groupId, String groupName) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, url + "/group/join." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, utf8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName==null?"":groupName, utf8));
		if (userIds != null) {
			for (String id : userIds) {
				sb.append("&userId=").append(URLEncoder.encode(id, utf8));
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 退出群
	 * @param userId
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult quitGroup(
			String userId, String groupId) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, url + "/group/quit." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		sb.append("&groupId=").append(URLEncoder.encode(groupId, utf8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 批量退出群
	 * @param userIds
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult quitGroupBatch(
			List<String> userIds, String groupId)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, url + "/group/quit." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, utf8));
		if (userIds != null) {
			for (String id : userIds) {
				sb.append("&userId=").append(URLEncoder.encode(id, utf8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 解散群
	 * @param userId
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult dismissGroup(
			String userId, String groupId) throws Exception {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, url
						+ "/group/dismiss." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		sb.append("&groupId=").append(URLEncoder.encode(groupId, utf8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 *  同步用户群信息
	 * @param userId
	 * @param groups
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult syncGroup(
			String userId, List<GroupInfo> groups)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret, url + "/group/sync." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, utf8));
		if (groups != null) {
			for (GroupInfo info : groups) {
				if (info != null) {
					sb.append(
							String.format("&group[%s]=",
									URLEncoder.encode(info.getId(), utf8)))
							.append(URLEncoder.encode(info.getName(), utf8));
				}
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 刷新群信息
	 * @param groupId
	 * @param groupName
	 * @return
	 */
	public static SdkHttpResult refreshGroupInfo( String groupId, String groupName
			) throws Exception {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, url
						+ "/group/refresh." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(groupId, utf8));
		sb.append("&groupName=").append(URLEncoder.encode(groupName==null?"":groupName, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 *  刷新群信息
	 * @param group
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult refreshGroupInfo( GroupInfo group)
			throws Exception {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, url
						+ "/group/refresh." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(URLEncoder.encode(group.getId(), utf8));
		sb.append("&groupName=").append(
				URLEncoder.encode(group.getName(), utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 发送消息
	 * @param fromUserId
	 * @param toUserIds
	 * @param msg
	 * @return
	 */
	public static SdkHttpResult publishMessage(
			String fromUserId, List<String> toUserIds, Message msg
			) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/message/private/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, utf8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(
						URLEncoder.encode(toUserIds.get(i), utf8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.toString(), utf8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
	
	/**
	 * 发送消息
	 * @param fromUserId
	 * @param toUserIds
	 * @param msg
	 * @return
	 */
	public static SdkHttpResult publishMessage(
			String fromUserId, String toUserIds, String msg
			) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/message/private/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, utf8));
		sb.append("&toUserId=").append(
				URLEncoder.encode(toUserIds, utf8));
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.toString(), utf8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 发送消息
	 * @param fromUserId
	 * @param toUserIds
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult publishMessage(
			String fromUserId, List<String> toUserIds, Message msg,
			String pushContent, String pushData)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/message/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, utf8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(
						URLEncoder.encode(toUserIds.get(i), utf8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.toString(), utf8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), utf8));

		if (pushContent != null) {
			sb.append("&pushContent=").append(
					URLEncoder.encode(pushContent==null?"":pushContent, utf8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData==null?"":pushData, utf8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 发送系统消息
	 * @param fromUserId
	 * @param toUserId
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @return
	 */
	public static SdkHttpResult publishSystemMessage(String fromUserId, String toUserId, Message msg, String pushContent, String pushData){
		List<String> toUserIds = new ArrayList<String>();
		
		toUserIds.add(toUserId);
		
		return publishSystemMessage(fromUserId,toUserIds,msg,pushContent,pushData);
	}
	/**
	 * 发送系统消息
	 * @param fromUserId
	 * @param toUserIds
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult publishSystemMessage(String fromUserId, List<String> toUserIds, Message msg, String pushContent, String pushData){
		HttpURLConnection conn;
		try {
			conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret, url + "/message/system/publish." + format.toString());
			
			StringBuilder sb = new StringBuilder();
			sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, utf8));
			if (toUserIds != null) {
				for (int i = 0; i < toUserIds.size(); i++) {
					sb.append("&toUserId=").append(
							URLEncoder.encode(toUserIds.get(i), utf8));
				}
			}
			sb.append("&objectName=").append(URLEncoder.encode(msg.getType(), utf8));
			
			sb.append("&content=").append(URLEncoder.encode(msg.toString(), utf8));

			if (pushContent != null) {
				sb.append("&pushContent=").append(
						URLEncoder.encode(pushContent==null?"":pushContent, utf8));
			}

			if (pushData != null) {
				sb.append("&pushData=").append(URLEncoder.encode(pushData==null?"":pushData, utf8));
			}

			HttpUtil.setBodyParameter(sb, conn);

			return HttpUtil.returnResult(conn);
			
		} catch (MalformedURLException e) {
			return new SdkHttpResult(-1, e.getMessage());
		} catch (ProtocolException e) {
			return new SdkHttpResult(-1, e.getMessage());
		} catch (IOException e) {
			return new SdkHttpResult(-1, e.getMessage());
		}catch (Exception e) {
			return new SdkHttpResult(-1, e.getMessage());
		}
		
	}

	/**
	 * 发送群消息
	 * @param fromUserId
	 * @param toGroupIds
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult publishGroupMessage( String fromUserId, List<String> toGroupIds,
			Message msg, String pushContent, String pushData)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/message/group/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, utf8));
		if (toGroupIds != null) {
			for (int i = 0; i < toGroupIds.size(); i++) {
				sb.append("&toGroupId=").append(
						URLEncoder.encode(toGroupIds.get(i), utf8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), utf8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), utf8));

		if (pushContent != null) {
			sb.append("&pushContent=").append(
					URLEncoder.encode(pushContent==null?"":pushContent, utf8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData==null?"":pushData, utf8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 发送聊天室消息
	 * @param fromUserId
	 * @param toChatroomIds
	 * @param msg
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult publishChatroomMessage( String fromUserId, List<String> toChatroomIds,
			Message msg) throws Exception {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, url
						+ "/message/chatroom/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, utf8));
		if (toChatroomIds != null) {
			for (int i = 0; i < toChatroomIds.size(); i++) {
				sb.append("&toChatroomId=").append(
						URLEncoder.encode(toChatroomIds.get(i), utf8));
			}
		}
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), utf8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
	
	/**
	 * 发送广播消息
	 * @param fromUserId
	 * @param msg
	 * @param pushContent
	 * @param pushData
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult broadcastMessage(
			String fromUserId, Message msg,String pushContent, String pushData) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/message/broadcast." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, utf8));		
		sb.append("&objectName=")
				.append(URLEncoder.encode(msg.getType(), utf8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), utf8));
		if (pushContent != null) {
			sb.append("&pushContent=").append(
					URLEncoder.encode(pushContent==null?"":pushContent, utf8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData==null?"":pushData, utf8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 创建聊天室
	 * @param chatrooms
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult createChatroom(
			List<ChatroomInfo> chatrooms) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/chatroom/create." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("1=1");
		if (chatrooms != null) {
			for (ChatroomInfo info : chatrooms) {
				if (info != null) {
					sb.append(
							String.format("&chatroom[%s]=",
									URLEncoder.encode(info.getId(), utf8)))
							.append(URLEncoder.encode(info.getName(), utf8));
				}
			}
		}
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 销毁聊天室
	 * @param chatroomIds
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult destroyChatroom( List<String> chatroomIds)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/chatroom/destroy." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("1=1");
		if (chatroomIds != null) {
			for (String id : chatroomIds) {
				sb.append("&chatroomId=").append(URLEncoder.encode(id, utf8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 查询聊天室信息
	 * @param chatroomIds
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult queryChatroom(
			List<String> chatroomIds) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/chatroom/query." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("1=1");
		if (chatroomIds != null) {
			for (String id : chatroomIds) {
				sb.append("&chatroomId=").append(URLEncoder.encode(id, utf8));
			}
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 获取消息历史记录下载地址
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult getMessageHistoryUrl( String date) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/message/history." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("date=").append(URLEncoder.encode(date, utf8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 删除消息历史记录
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult deleteMessageHistory( String date) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/message/history/delete." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("date=").append(URLEncoder.encode(date, utf8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
	
	/**
	 *  获取群内成员
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult queryGroupUserList( String groupId)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/group/user/query." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(
				URLEncoder.encode(groupId == null ? "" : groupId, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
	
	/**
	 * 添加禁言群成员
	 * @param groupId
	 * @param userId
	 * @param minute
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult groupUserGagAdd( String groupId, String userId, long minute
			) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/group/user/gag/add." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(
				URLEncoder.encode(groupId == null ? "" : groupId, utf8));
		sb.append("userId=").append(
				URLEncoder.encode(userId == null ? "" : userId, utf8));
		sb.append("minute=").append(
				URLEncoder.encode(String.valueOf(minute), utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 移除禁言群成员
	 * @param groupId
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult groupUserGagRollback( String groupId, String userId)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/group/user/gag/rollback." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(
				URLEncoder.encode(groupId == null ? "" : groupId, utf8));
		sb.append("userId=").append(
				URLEncoder.encode(userId == null ? "" : userId, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 查询被禁言群成员
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult groupUserGagList( String groupId)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/group/user/gag/list." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("groupId=").append(
				URLEncoder.encode(groupId == null ? "" : groupId, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 添加敏感词
	 * @param word
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult wordFilterAdd(
			String word) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/wordfilter/add." + format.toString());

		if (word == null || word.length() == 0) {
			throw new Exception("word is not null or empty.");
		}
		StringBuilder sb = new StringBuilder();
		sb.append("word=").append(
				URLEncoder.encode(word == null ? "" : word, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 移除敏感词
	 * @param word
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult wordFilterDelete( String word) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,
				appSecret,
				url + "/wordfilter/delete." + format.toString());

		if (word == null || word.length() == 0) {
			throw new Exception("word is not null or empty.");
		}
		StringBuilder sb = new StringBuilder();
		sb.append("word=").append(
				URLEncoder.encode(word == null ? "" : word, utf8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	/**
	 * 查询敏感词列表 
	 * @return
	 * @throws Exception
	 */
	public static SdkHttpResult wordFilterList(
			) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey,appSecret,url + "/wordfilter/list." + format.toString());
		StringBuilder sb = new StringBuilder();
		sb.append("1=1");
		HttpUtil.setBodyParameter(sb, conn);
		return HttpUtil.returnResult(conn);
	}
}
